package com.qq.view;

import com.qq.service.QQClientService;
import com.qq.utils.Utility;

public class QQView {
    private boolean loop = true;
    private boolean subLoop = true;
    private int key;
    private final QQClientService clientService = new QQClientService();

    /**
     * 功能：显示主菜单界面
     */
    public void mainMenu() {
        outer:
        while (loop) {
            System.out.println("---------------欢迎登录网络通信系统---------------");
            System.out.println("\t\t\t\t  1. 登录系统");
            System.out.println("\t\t\t\t  9. 退出系统");
            System.out.print("请输入相应功能序号：");
            key = Utility.readInt(1);
            switch (key) {
                case 1:
                    System.out.print("请输入账号：");
                    String userId = Utility.readString(50);
                    System.out.print("请输入密码：");
                    String passwd = Utility.readString(50);
                    //等待服务器返回结果来判断是否登录成功，成功就进入二级界面
                    if (clientService.login(userId, passwd)) {
                        System.out.println("欢迎用户：" + userId);
                        while (subLoop) {
                            System.out.println("---------------网络通信系统二级菜单---------------");
                            System.out.println("用户：" + userId);
                            System.out.println("\t1. 显示在线用户列表");
                            System.out.println("\t2. 群发消息");
                            System.out.println("\t3. 私聊消息");
                            System.out.println("\t4. 发送文件");
                            System.out.println("\t9. 退出登录");

                            System.out.print("请输入相应功能序号：");
                            key = Utility.readInt(1);
                            switch (key) {
                                case 1:
                                    clientService.onlineUsersList();
                                    try {
                                        Thread.sleep(500);
                                    } catch (InterruptedException e) {
                                        throw new RuntimeException(e);
                                    }
                                    while (key != 0) {
                                        System.out.print("输入0退出：");
                                        key = Utility.readInt(1);
                                    }
                                    break;
                                case 2:
                                    System.out.print("请输入群发消息内容：");
                                    String contentToAll = Utility.readString(100);
                                    clientService.sendMessageToAll(contentToAll);
                                    break;
                                case 3:
                                    System.out.print("请输入发送对象：");
                                    String getterId = Utility.readString(50);
                                    System.out.print("请输入发送内容：");
                                    String content = Utility.readString(100);
                                    clientService.sendMessage(getterId, content);
                                    break;
                                case 4:
                                    System.out.print("请输入要发送文件的路径：");
                                    String path = Utility.readString(150);
                                    System.out.print("请输入接收者ID：");
                                    String fileGetter = Utility.readString(50);
                                    System.out.println("1. 文本文件");
                                    System.out.println("2. 二进制文件");
                                    System.out.print("请输入类型序号：");
                                    int fileType = Utility.readInt(1);
                                    switch (fileType) {
                                        case 1:
                                            clientService.sendFile(fileGetter, path, "文本文件");
                                            break;
                                        case 2:
                                            clientService.sendFile(fileGetter, path, "二进制文件");
                                            break;
                                    }
                                    System.out.println("文件已发送！");
                                    break;
                                case 9:
                                    clientService.logOut();
                                    subLoop = false;
                                    System.out.println("账号退出！");
                                    break;
                            }
                        }
                    } else {
                        System.out.println("登录服务器失败！");
                    }
                    break;
                case 9:
                    loop = false;
                    System.out.println("系统退出！");
                    break outer;
            }
        }
    }
}
